###########################################################################
#
#   Copyright 2010 American Public Media Group
#
#   This file is part of AIR2.
#
#   AIR2 is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   AIR2 is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with AIR2.  If not, see <http://www.gnu.org/licenses/>.
#
###########################################################################

package AIR2::Organization;
use strict;
use base qw(AIR2::DB);
use Carp;

__PACKAGE__->meta->setup(
    table => 'organization',

    columns => [
        org_id   => { type => 'serial', not_null => 1 },
        org_uuid => {
            type     => 'character',
            length   => 12,
            not_null => 1
        },
        org_name => {
            type     => 'varchar',
            length   => 128,
            not_null => 1
        },
        org_logo_uri       => { type => 'varchar', length => 255 },
        org_display_name   => { type => 'varchar', length => 128 },
        org_parent_id      => { type => 'integer' },
        org_default_prj_id => { type => 'integer' },
        org_type           => {
            type     => 'character',
            default  => 'N',
            length   => 1,
            not_null => 1
        },
        org_status => {
            type     => 'character',
            default  => 'A',
            length   => 1,
            not_null => 1
        },
        org_html_color => {
            type     => 'character',
            default  => '000000',
            length   => 6,
            not_null => 1
        },
        org_cre_user => { type => 'integer', not_null => 1 },
        org_upd_user => { type => 'integer' },
        org_cre_dtim => {
            type     => 'datetime',
            not_null => 1
        },
        org_upd_dtim => { type => 'datetime' },
    ],

    primary_key_columns => ['org_id'],

    unique_key => ['org_uuid'],

    foreign_keys => [
        cre_user => {
            class       => 'AIR2::User',
            key_columns => { org_cre_user => 'user_id' },
        },

        parent => {
            class       => 'AIR2::Organization',
            key_columns => { org_parent_id => 'org_id' },
        },

        upd_user => {
            class       => 'AIR2::User',
            key_columns => { org_upd_user => 'user_id' },
        },

        default_project => {
            class       => 'AIR2::Project',
            key_columns => { org_default_prj_id => 'prj_id' },
        }
    ],

    relationships => [
        org_sys_id => {
            class      => 'AIR2::OrgSysId',
            column_map => { org_id => 'osid_org_id' },
            type       => 'one to many',
        },

        children => {
            class      => 'AIR2::Organization',
            column_map => { org_id => 'org_parent_id' },
            type       => 'one to many',
        },

        project_org => {
            class      => 'AIR2::ProjectOrg',
            column_map => { org_id => 'porg_org_id' },
            type       => 'one to many',
        },

        src_activity => {
            class      => 'AIR2::SrcActivity',
            column_map => { org_id => 'sact_org_id' },
            type       => 'one to many',
        },

        src_org => {
            class      => 'AIR2::SrcOrg',
            column_map => { org_id => 'so_org_id' },
            type       => 'one to many',
        },

        src_pref_org => {
            class      => 'AIR2::SrcPrefOrg',
            column_map => { org_id => 'spo_org_id' },
            type       => 'one to many',
        },

        user_org => {
            class      => 'AIR2::UserOrg',
            column_map => { org_id => 'uo_org_id' },
            type       => 'one to many',
        },
    ],
);

=head2 get_active_parents_ids

Similar to the method in the Organization.php Doctrine model.
Returns array ref of org_ids for all active parents.

=cut

sub get_active_parents_ids {
    my $self = shift;
    my $pids = [];
    if ( $self->org_status ne 'A' ) {
        return $pids;
    }
    my $parent = $self->parent;
    while ( $parent && $parent->org_status eq 'A' ) {
        last if $parent->org_id == $self->org_id;
        push @$pids, $parent->org_id;
        $parent = $parent->parent;
    }
    return $pids;
}

sub collect_related_org_ids {
    my $self = shift;
    my $org_ids = shift or croak "org_ids required";

    if ( $self->org_status eq 'A' ) {
        push @$org_ids, $self->org_id;
        for my $oid ( @{ $self->get_active_parents_ids() } ) {
            push @$org_ids, $oid;
        }
        for my $o ( @{ $self->children } ) {
            next if $o->org_id == $self->org_id;
            $o->collect_related_org_ids($org_ids);
        }
    }
}

1;
